{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<form class="layui-form p-4">
    <h3 class="pb-3">查看固定资产工时</h3>

    <!-- <div>
        <form class="layui-form gg-form-bar border-x border-t" lay-filter="barsearchform2">


            <div class="layui-input-inline" style="width:136px">
                <input type="text" class="layui-input" id="layui-input-time" lay-verify="required" placeholder="请选择月份"
                    name="layui-input-time">
            </div>
            <div class="layui-input-inline" style="width:150px;">
                <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform"><i
                        class="layui-icon layui-icon-search mr-1"></i>搜索
                </button>
            </div>
        </form>
    </div> -->
    <div class="p-4">
        <table id="activeUser" lay-filter="activeUser"></table>
    </div>
    <!-- <div class="py-3">
        <input type="hidden" name="scene" value="add">
        <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform">立即提交</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div> -->
</form>
<script type="text/html" id="ID-table-demo-css-tool">
    <div class="layui-form layui-form-testcss">
      <div class="layui-input-wrap">
        <input name="AAA" value="{{= d.AAA || '' }}" lay-affix="clear" placeholder="表单 1" class="layui-input" >
      </div>
    </div>
  </script>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>



    const moduleInit = ['tool', 'employeepicker',];

    function gouguInit() {
        const currentDate = new Date();
        let dateTime = `{$_GET.year}-{$_GET.month}`;
        let eid = "{$_GET.eid}";

        var table = layui.table, tool = layui.tool, form = layui.form, laydate = layui.laydate;
        let tableTitle = [
        { field: 'name', title: '固定资产姓名', totalRowText: '合计' },
            { field: 'all_hours', title: '当月总工时'},
            { field: 'totalRow', title: '合计(小时)', },];
        laydate.render({
            elem: "#layui-input-time",
            type: "month",
            id: 'layui-input-time',
            value: dateTime,
            autoConfirm: true,
            done: function (value, date, endDate) {
                dateTime = value;
                table.reload('avtiveUser', {
                    where: {
                        time: dateTime,
                        eid: eid,
                    },
                })
                return;
            }
        });

        table.render({
            elem: '#activeUser',
            id: 'avtiveUser',
            url: "/home/EquipmentWorkHours/getEquItem",
            totalRow: true,
            where: {
                time: dateTime,
                eid: eid,
            },
            cols: [tableTitle],
            parseData: function (res) { // res 即为原始返回的数据
                // table.reload(id)
                let resTitle = res.data.item;
                let resData = res.data.user;
                let dataTitle = [];
                resTitle.forEach(e => {
                    dataTitle.push({ field: `item_` + e.id, title: e.name,totalRow: true})
                });
                resData.map(e => {
                    e.totalRow = 0;
                    e.item.forEach(fid => {
                        e[`item_${fid.id}`] = fid.work_hours * 1 || 0;
                        e.totalRow += fid.work_hours || 0

                    })
                    // e.totalRow = e.item.reduce((a, b) => {
                    //         return a.work_hours + b.work_hours;
                    //     })

                })

                table.init('activeUser', {
                    cols: [
                        [...tableTitle, ...dataTitle]
                    ], data: [...resData],totalRow: true,
                    done: function (res, page, count) {
                        // 设置合计行
                        var divArr = $('.layui-table-total td');
                        $.each(divArr, function (index, item) {
                            var _div = $(item);
                            var content = _div.html();
                            content = content.replace('.00', "");
                            _div.html(content)
                        })

                    },
                });
                return res;
            },
        })


        table.on('edit(activeUser)', function (obj) {
            var value = obj.value;
            var data = obj.data // 得到所在行所有键值
            let add = data.item.map((e) => {
                return data[`item_${e.id}`]
            }).reduce((prev, cur) => {
                return prev * 1 + cur * 1;
            })
            data.totalRow = add;
            obj.update(data, true)
            console.log(data, add)

            // 值的校验
            if (value.replace(/\s/g, '') === '') {
                layer.tips('值不能为空', this, { tips: 1 });
                return obj.reedit(); // 重新编辑 -- v2.8.0 新增
            }
            // 编辑后续操作，如提交更新请求，以完成真实的数据更新
            // …

            // 显示 - 仅用于演示

        });

        //监听提交
        form.on('submit(webform)', function (data) {

            let formData = table.getData('activeUser');
            let user_work_hours_value = [];
            formData.forEach(e => {
                e.item.forEach(a => {
                    let obj = {};
                    obj.eq_id = e.id;
                    obj.item_id = a.id;
                    obj.all_hours = e.all_hours || 0;
                    obj.work_hours = e[`item_${a.id}`];
                    user_work_hours_value.push(obj)
                })

            })
            let user_work_data = {
                time: dateTime,
                eid: eid,
                user_work_hours_value
            }
            // var inputs = $('input[name="user_work_hours"]');
            // var values = [];
            // inputs.each(function () {
            //     let obj = new Object();
            //     obj[this.name] = this.value;
            //     obj['uid'] = $(this).data('id');
            //     obj['item_id'] = $(this).data('itemid');

            //     // 如果values对象中还没有这个name，就初始化它为一个数组
            //     if (!values) {
            //         values = [];
            //     }

            //     // 将value添加到对应的数组中
            //     values.push(obj);

            // });

            // // 将对象转换为JSON字符串
            // var user_work_hours = JSON.stringify(values);

            // data.field['user_work_hours_value'] = user_work_hours;

            let callback = function (e) {
                layer.msg(e.msg);
                if (e.code == 0) {
                    tool.sideClose(1000);

                }
            }
            tool.post("/home/EquipmentWorkHours/operation", user_work_data, callback);
            return false;
        });



    }
</script>
{/block}
<!-- /脚本 -->